<template>
  <div class="app-container">
    <!--搜索栏 开始-->
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="文章标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入文章标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">刷新</el-button>
      </el-form-item>
    </el-form>
    <!--搜索栏 结束-->

    <!--操作栏 开始-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mini:wenzhang:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>
    <!--操作栏 结束-->

    <el-table v-loading="loading" :data="tableData">
      <el-table-column
        sortable
        align="center"
        prop="title"
        label="标题"
      >
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        prop="type"
        label="类型"
      >
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        label="渠道"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.channel == 0">公共板块</span>
          <span v-if="scope.row.channel == 1">代理体系</span>
          <span v-if="scope.row.channel == 2">技能培训</span>
        </template>
      </el-table-column>

      <el-table-column label="缩略图" width="100">
        <!--        自定义表格内容-->
        <template slot-scope="scope">
          <!-- scope.row 包含表格里当前行的所有数据 -->
          <img :src="imgPrex+scope.row.url" style="width: 80px;height: 80px;">
        </template>
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        prop="userRead"
        label="阅读量"
      >
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        prop="sort"
        label="排序"
      >
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        prop="auth"
        label="作者"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        prop="area"
        label="工作室"
      >
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        prop="createTime"
        label="时间"
      >
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>

import config from '@/utils/config'

import {
  listMiniWenzhang,
  getMiniWenzhang,
  delMiniWenzhang,
  addMiniWenzhang,
  updateMiniWenzhang,
  exportMiniWenzhang
} from '@/api/mini/wenzhang'
import { changeUserStatus } from '@/api/system/user'

export default {
  name: 'MiniWenzhang',
  data() {
    return {
      imgPrex: config.imgPrefx,
      loading: true,
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      total: 0,
      tableData: [],
      title: '',
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      form: {},
      rules: {
        id: [
          { required: true, message: '文章主键不能为空', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '文章标题不能为空', trigger: 'blur' }
        ],
        read: [
          { required: true, message: '阅读量不能为空', trigger: 'blur' }
        ],
        zan: [
          { required: true, message: '点赞数不能为空', trigger: 'blur' }
        ],
        friend: [
          { required: true, message: '在看朋友数量不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '文章类型（原创，转载）不能为空', trigger: 'blur' }
        ],
        auth: [
          { required: true, message: '作者不能为空', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '区域不能为空', trigger: 'blur' }
        ],
        time: [
          { required: true, message: '时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.resetForm()
      listMiniWenzhang(this.queryParams).then(response => {
        this.tableData = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.resetForm()
    },
    resetForm() {
      this.form = {}
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ name: 'WenzhangModify' })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const keyId = row.id || this.ids
      this.$router.push({
        name: 'WenzhangModify',
        params: {
          keyId: keyId
        }
      })
    },
    /** 启用，停用 */
    handleStatusChange(row) {
      let text = row.status === '0' ? '启用' : '停用'
      this.$modal.confirm('确认要' + text + "该记录吗？").then(function() {
        return updateMiniWenzhang({
          "id":row.id,
          "status":row.status
        })
      }).then(() => {
        this.$modal.msgSuccess(text + '成功')
      }).catch(function() {
        row.status = row.status === '0' ? '1' : '0'
      })
    },
    submitForm: function() {
      this.$refs['checkForm'].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMiniWenzhang(this.form).then(response => {
              if ('0' === response.code) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addMiniWenzhang(this.form).then(response => {
              if ('0' === response.code) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const keyIds = row.id || this.ids
      this.$confirm('是否确认删除岗位编号为"' + keyIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delMiniWenzhang(keyIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    }
    /** 导出按钮操作 */
    /*  handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有岗位数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportRole(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }*/
  }
}
</script>
